package com.jn.mvvm.views;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jn.mvvm.databinding.ActivityIntroduceBinding;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jn.mvvm.R;

import com.jn.mvvm.entity.PetEntity;


public class IntroduceActivity extends AppCompatActivity {
    private ActivityIntroduceBinding binding ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_introduce) ;
        Intent intent = getIntent() ;
        Gson gson = new Gson() ;
        String jsonStr = intent.getStringExtra("pet") ;
        PetEntity.ResultBean.ListBean pet = gson.fromJson(jsonStr, PetEntity.ResultBean.ListBean.class);
        binding.setPetInfo(pet);
        binding.iTv1.setText("名字："+pet.getName()+"     英文名："+pet.getEngName());
        binding.iTv2.setText("国籍："+pet.getNation()+"     价格："+pet.getPrice());
        binding.iTv3.setText("寿命："+pet.getLife()+"     易得病："+pet.getEasyOfDisease());
        binding.iTv9.setText("性格："+pet.getCharacters());
        Glide.with(this).load(pet.getCoverURL()).into(binding.iImg);
        binding.iImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openWebsite(pet.getUrl());
            }
        });
    }


    private void openWebsite(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(this, "无法打开网页，请检查设备是否安装了浏览器", Toast.LENGTH_SHORT).show();
        }
    }
}
